package hate.base;
import hate.util.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 添加过滤的方法：
 * 1.所有jwe目录里面的请求都要过滤,如下
 * interceptorRegistration.addPathPatterns("/jwe/**");
 * 2.error页面全部放过
 * 3.登录页面放过
 * 4.过滤器错误返回登录页面，如下
 * response.sendRedirect( "/dede/");
 *
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        InterceptorRegistration interceptorRegistration = registry.addInterceptor(new MyInterceptor());
        /*同一异常和无界面*/
        interceptorRegistration.excludePathPatterns("/error");
        interceptorRegistration.excludePathPatterns("/login/loginCheck");
//      interceptorRegistration.excludePathPatterns("/**");
        interceptorRegistration.excludePathPatterns("/index");

        interceptorRegistration.addPathPatterns("/jwe/login/getToken");
        //不监控如下链接
        //interceptorRegistration.excludePathPatterns("/**");

        //监控所有如下链接的
        interceptorRegistration.addPathPatterns("/login/getToken");
        interceptorRegistration.excludePathPatterns("/**");
        //interceptorRegistration.addPathPatterns("/**");


    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //需要配置1：需要告知系统，这是要被当成静态文件的！
        //第一个方法设置访问路径前缀，第二个方法设置资源路径
        registry.addResourceHandler("/static/css/**")
        .addResourceLocations("classpath:/static/");
    }

    private class MyInterceptor implements HandlerInterceptor {

        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
            request.getRequestURI();
            System.out.println("在控制器执行前调用 ");
            System.out.println("request.getRequestURI()="+request.getRequestURI());
            
            Object account = request.getSession().getAttribute("account");
            Object token = request.getSession().getAttribute("token");
            System.out.println("account="+account);
            System.out.println("token="+token);
            System.out.println("StringUtils.map.get(account)="+StringUtils.map.get(account));
            if (account == null) {
                System.out.println("当前用户未登录!");
                response.sendRedirect( "/mgmt/login.html");
                return false;
            }else if(token.equals(StringUtils.map.get(account))){

                System.out.println("当前用户已登录，登录的用户名为： " + account);

            }
            System.out.println("当前用户已登录，登录的用户名为： " + account);
            return true;
        }

        @Override
        public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
            //controller方法处理完毕后，调用此方法
            System.out.println("在后端控制器执行后调用 ");
        }

        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
            //页面渲染完毕后调用此方法
            System.out.println("整个请求执行完成后调用 ");
        }
    }
}
